package se.perhamrin.judo.persistance;

import java.util.Collection;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

import se.perhamrin.judo.domain.Day;
import se.perhamrin.judo.domain.TestMember;
import se.perhamrin.judo.domain.Time;
import se.perhamrin.judo.domain.UjkMember;
import se.perhamrin.judo.domain.UjkSession;

public class CheckinPersister implements JudoCheckinPersister {

	private final DataSource ujkDatabase;

	public CheckinPersister(DataSource ujkDatabase) {
		this.ujkDatabase = ujkDatabase;
	}

	public void addNewMember(Long idrottOnlineId, String name) {
		SimpleJdbcInsert insert = new SimpleJdbcInsert(ujkDatabase);
		insert.setTableName("members");
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue(MemberRowMapper.IO_NUMBER, idrottOnlineId);
		params.addValue(MemberRowMapper.NAME, name);
		insert.execute(params);
	}

	public Collection<UjkMember> getAllMembers() {
		SimpleJdbcTemplate query = new SimpleJdbcTemplate(ujkDatabase);
		String sql = "select * from  members";
		List<UjkMember> members = query.query(sql, new MemberRowMapper());
		return members;
	}

	public void addJudoSession(String description, Day day, Time start, Time end) {
		MapSqlParameterSource param = new MapSqlParameterSource();
		param.addValue(SessionRowMapper.DESCRIPTION, description);
		param.addValue(SessionRowMapper.SESSION_DAY, day.name());
		param.addValue(SessionRowMapper.START_TIME, start.toString());
		param.addValue(SessionRowMapper.END_TIME, end.toString());

		SimpleJdbcInsert insert = new SimpleJdbcInsert(ujkDatabase);
		insert.setTableName("sessions");
		insert.execute(param);
	}

	public UjkSession getSession(Long sessionId) {
		throw new RuntimeException("not implemented");
	}

	public void changeSessionDescription(Long sessionId, String description) {
		throw new RuntimeException("not implemented");
	}

	public void changeSessionTime(Long sessionId, Day day, Time start, Time end) {
		throw new RuntimeException("not implemented");
	}

	public Collection<TestMember> getTestMembers() {
		throw new RuntimeException("not implemented");
	}
}
